package com.nanyang.tms.manager.saleprice.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.json.JSONUtil;
import com.nanyang.common.core.annotation.SystemLog;
import com.nanyang.common.core.dict.CommonEnum;
import com.nanyang.common.core.page.PageBean;
import com.nanyang.common.core.page.PageTableBean;
import com.nanyang.common.tool.utils.convert.BeanUtils;
import com.nanyang.common.tool.utils.uuid.GlobalIdGeneratorUtil;
import com.nanyang.tms.common.contstant.TmsSerializeConstant;
import com.nanyang.tms.common.dict.TmsCommonEnum;
import com.nanyang.tms.common.dict.TmsExceptionEnum;
import com.nanyang.tms.common.exception.TmsManagerException;
import com.nanyang.tms.common.util.TmsExceptionUtil;
import com.nanyang.tms.dao.saleprice.*;
import com.nanyang.tms.domain.saleprice.*;
import com.nanyang.tms.manager.saleprice.SalePriceManager;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

@Slf4j
@Component("salePriceManager")
public class SalePriceManagerImpl implements SalePriceManager {
    @Autowired
    private SalePriceDao salePriceDao;
    @Autowired
    private SalePartCityDao salePartCityDao;
    @Autowired
    private SalePartCountyDao salePartCountyDao;
    @Autowired
    private SalePartDao salePartDao;
    @Autowired
    private SalePartSchemeDao salePartSchemeDao;
    @Autowired
    private SalePartZipcodeDao salePartZipcodeDao;
    @Autowired
    private SalePriceRecordDao salePriceRecordDao;
    @Autowired
    private SaleWeightDao saleWeightDao;
    @Autowired
    private SaleWeightRecordDao saleWeightRecordDao;
    @Autowired
    private SalePriceDiscountDao salePriceDiscountDao;
    @Autowired
    private GlobalIdGeneratorUtil globalIdGeneratorUtil;

    @Override
    public PageBean<SalePriceVO> listSale(SalePriceQuery query) {
        try {
            if (query == null) {
                log.error(TmsCommonEnum.PARAM_IS_NULL.getCode() + TmsCommonEnum.PARAM_IS_NULL.getMessage());
                throw new TmsManagerException(TmsCommonEnum.PARAM_IS_NULL.getCode() ,TmsCommonEnum.PARAM_IS_NULL.getMessage());
            }
            Integer count = salePriceDao.countSale(query);
            PageBean<SalePriceVO> page = new PageTableBean<SalePriceVO>(query.getPageIndex(), query.getPageSize());
            if (null != count && count != 0) {
                page.setTotalItem(count);
                int startRow = page.getStartRow();
                if (startRow > 0) {
                    startRow -= 1;
                }
                query.setStartRow(startRow);
                page.addAll(salePriceDao.listSale(query));
            }
            return page;
        } catch (Exception e) {
            throw TmsExceptionUtil.wrapException(e);
        }
    }
    @SystemLog(module =SystemLog.Module.PRICE,template="priceAdd")
    @Override
    @Transactional
    public void saveSale(SaleBaseVO saleBaseVO) {
        try {
            SalePriceVO salePriceVO = saleBaseVO.getSalePriceVO();
            SalePriceQuery query=new SalePriceQuery();
            query.setSaleProductCode(salePriceVO.getSaleProductCode());
            if (salePriceDao.queryVerifySale(query)>0){
                throw new TmsManagerException(TmsExceptionEnum.SALEPRODUCT_IS_EXIST.getCode(),TmsExceptionEnum.SALEPRODUCT_IS_EXIST.getMessage());
            };
            SalePriceDO salePriceDO=new SalePriceDO();//设置销售价格基本信息
            BeanUtils.copyProperties(saleBaseVO.getSalePriceVO(),salePriceDO);
            String saleCode= TmsSerializeConstant.SALEPRICE_KEY+globalIdGeneratorUtil.getNextSeq(TmsSerializeConstant.SALEPRICE_SEQ_KEY, TmsSerializeConstant.SEQ_INCRBY);
            salePriceDO.setSaleCode(saleCode);
            salePriceDO.setGmtCreatePin(saleBaseVO.getGmtCreatePin());
            salePriceDO.setIp(saleBaseVO.getIp());
            salePriceDao.insert(salePriceDO);

            SalePartSchemeDO salePartSchemeDO=new SalePartSchemeDO();//分区方案
            BeanUtils.copyProperties(saleBaseVO.getSalePartSchemeVO(),salePartSchemeDO);
            String partSchemeCode= TmsSerializeConstant.SALEPRICE_PART_SCHEME_KEY+globalIdGeneratorUtil.getNextSeq(TmsSerializeConstant.SALEPRICE_PART_SCHEME_SEQ_KEY, TmsSerializeConstant.SEQ_INCRBY);
            salePartSchemeDO.setPartSchemeCode(partSchemeCode);
            salePartSchemeDO.setSaleCode(saleCode);
            salePartSchemeDO.setGmtCreatePin(saleBaseVO.getGmtCreatePin());
            salePartSchemeDO.setIp(saleBaseVO.getIp());
            salePartSchemeDao.insert(salePartSchemeDO);

            List<String> countyList=new ArrayList<>();//国家重复
            List<String> repCountyList=new ArrayList<>();//国家重复

            Map<String,SalePartDO> salePartMap=new HashMap<>();
            List<SalePartVO> salePartVOList = saleBaseVO.getSalePartSchemeVO().getSalePartVOList();
            salePartVOList.forEach(salePartVO ->{
                SalePartDO salePartDO=new SalePartDO();//分区列表信息
                BeanUtils.copyProperties(salePartVO,salePartDO);
                String partCode= TmsSerializeConstant.SALEPRICE_PART_KEY+globalIdGeneratorUtil.getNextSeq(TmsSerializeConstant.SALEPRICE_PART_SEQ_KEY, TmsSerializeConstant.SEQ_INCRBY);
                salePartDO.setPartCode(partCode);
                salePartDO.setPartSchemeCode(partSchemeCode);
                salePartDO.setGmtCreatePin(saleBaseVO.getGmtCreatePin());
                salePartDO.setIp(saleBaseVO.getIp());
                salePartDao.insert(salePartDO);
                salePartMap.put(salePartVO.getPartNo(),salePartDO);
                List<SalePartCountyVO> salePartCountyVOList = salePartVO.getSalePartCountyVOList();
                salePartCountyVOList.forEach(salePartCountyVO ->{
                    SalePartCountyDO salePartCountyDO=new SalePartCountyDO();//分区国家列表信息
                    BeanUtils.copyProperties(salePartCountyVO,salePartCountyDO);
                    String partCountyCode= TmsSerializeConstant.SALEPRICE_PART_COUNTY_KEY+globalIdGeneratorUtil.getNextSeq(TmsSerializeConstant.SALEPRICE_PART_COUNTY_SEQ_KEY, TmsSerializeConstant.SEQ_INCRBY);
                    salePartCountyDO.setPartCountyCode(partCountyCode);
                    salePartCountyDO.setPartCode(partCode);
                    salePartCountyDO.setGmtCreatePin(saleBaseVO.getGmtCreatePin());
                    salePartCountyDO.setIp(saleBaseVO.getIp());
                    salePartCountyDO.setPartSchemeCode(partSchemeCode);
                    salePartCountyDao.insert(salePartCountyDO);
                    if(countyList.contains(salePartCountyVO.getCountyName())){
                        repCountyList.add(salePartCountyVO.getCountyName());
                    }else{
                        countyList.add(salePartCountyVO.getCountyName());
                    }
                    List<SalePartCityVO> salePartCityVOList = salePartCountyVO.getSalePartCityVOList();
                    if(CollectionUtil.isNotEmpty(salePartCityVOList)){
                        salePartCityVOList.forEach(salePartCityVO ->{
                            SalePartCityDO salePartCityDO=new SalePartCityDO();//分区城市列表信息
                            BeanUtils.copyProperties(salePartCityVO,salePartCityDO);
                            String partCityCode= TmsSerializeConstant.SALEPRICE_PART_CITY_KEY+globalIdGeneratorUtil.getNextSeq(TmsSerializeConstant.SALEPRICE_PART_CITY_SEQ_KEY, TmsSerializeConstant.SEQ_INCRBY);
                            salePartCityDO.setPartCityCode(partCityCode);
                            salePartCityDO.setPartCountyCode(partCountyCode);
                            salePartCityDO.setGmtCreatePin(saleBaseVO.getGmtCreatePin());
                            salePartCityDO.setIp(saleBaseVO.getIp());
                            salePartCityDao.insert(salePartCityDO);
                        });
                    }
                    List<SalePartZipcodeVO> salePartZipcodeVOList = salePartCountyVO.getSalePartZipcodeVOList();
                    if(CollectionUtil.isNotEmpty(salePartZipcodeVOList)){
                        salePartZipcodeVOList.forEach(salePartZipcodeVO ->{
                            SalePartZipcodeDO salePartZipcodeDO=new SalePartZipcodeDO();//分区邮编列表信息
                            BeanUtils.copyProperties(salePartZipcodeVO,salePartZipcodeDO);
                            String partZipCode= TmsSerializeConstant.SALEPRICE_PART_ZIPCODE_KEY+globalIdGeneratorUtil.getNextSeq(TmsSerializeConstant.SALEPRICE_PART_ZIPCODE_SEQ_KEY, TmsSerializeConstant.SEQ_INCRBY);
                            salePartZipcodeDO.setPartZipCode(partZipCode);
                            salePartZipcodeDO.setPartCountyCode(partCountyCode);
                            salePartZipcodeDO.setGmtCreatePin(saleBaseVO.getGmtCreatePin());
                            salePartZipcodeDO.setIp(saleBaseVO.getIp());
                            salePartZipcodeDao.insert(salePartZipcodeDO);
                        });
                    }

                });

            });

            if(CollectionUtil.isNotEmpty(repCountyList)){//国家存在重复的
                throw new TmsManagerException(TmsExceptionEnum.COUNTY_IS_EXIST.getCode(),String.format(TmsExceptionEnum.COUNTY_IS_EXIST.getMessage(), JSONUtil.toJsonStr(repCountyList)));
            }


            SaleWeightDO saleWeightDO=new SaleWeightDO();//重量模板信息
            BeanUtils.copyProperties(saleBaseVO.getSaleWeightVO(),saleWeightDO);
            String weightCode= TmsSerializeConstant.SALEPRICE_WEIGHT_KEY+globalIdGeneratorUtil.getNextSeq(TmsSerializeConstant.SALEPRICE_WEIGHT_SEQ_KEY, TmsSerializeConstant.SEQ_INCRBY);
            saleWeightDO.setWeightCode(weightCode);
            saleWeightDO.setSaleCode(saleCode);
            saleWeightDO.setGmtCreatePin(saleBaseVO.getGmtCreatePin());
            saleWeightDO.setIp(saleBaseVO.getIp());
            saleWeightDao.insert(saleWeightDO);

            Map<String,SaleWeightRecordDO> saleWeightRecordMap=new HashMap<>();
            List<SaleWeightRecordVO> saleWeightRecordVOList = saleBaseVO.getSaleWeightVO().getSaleWeightRecordVOList();
            saleWeightRecordVOList.forEach(saleWeightRecordVO ->{
                SaleWeightRecordDO saleWeightRecordDO=new SaleWeightRecordDO();//重量模板明细
                BeanUtils.copyProperties(saleWeightRecordVO,saleWeightRecordDO);
                String weightRecordCode= TmsSerializeConstant.SALEPRICE_WEIGHT_RECORD_KEY+globalIdGeneratorUtil.getNextSeq(TmsSerializeConstant.SALEPRICE_WEIGHT_RECORD_SEQ_KEY, TmsSerializeConstant.SEQ_INCRBY);
                saleWeightRecordDO.setWeightRecordCode(weightRecordCode);
                saleWeightRecordDO.setWeightCode(weightCode);
                saleWeightRecordDO.setGmtCreatePin(saleBaseVO.getGmtCreatePin());
                saleWeightRecordDO.setIp(saleBaseVO.getIp());
                saleWeightRecordDao.insert(saleWeightRecordDO);
                saleWeightRecordMap.put(saleWeightRecordVO.getWeightRecordNo(),saleWeightRecordDO);
            });


            List<SalePriceRecordVO> salePriceRecordVOList = saleBaseVO.getSalePriceRecordVOList();
            salePriceRecordVOList.forEach(salePriceRecordVO ->{
                SalePriceRecordDO salePriceRecordDO=new SalePriceRecordDO();//销售价格明细
                String priceRecordCode= TmsSerializeConstant.SALEPRICE_PRICE_RECORD_KEY+globalIdGeneratorUtil.getNextSeq(TmsSerializeConstant.SALEPRICE_PRICE_RECORD_SEQ_KEY, TmsSerializeConstant.SEQ_INCRBY);
                salePriceRecordDO.setPrice(salePriceRecordVO.getPrice());
                SalePartDO salePartDO = salePartMap.get(salePriceRecordVO.getPartNo());
                if(ObjectUtil.isNotNull(salePartDO)){
                    salePriceRecordDO.setPartCode(salePartDO.getPartCode());
                    salePriceRecordDO.setPartName(salePartDO.getPartName());
                    salePriceRecordDO.setPartSchemeCode(salePartDO.getPartSchemeCode());
                }
                SaleWeightRecordDO saleWeightRecordDO = saleWeightRecordMap.get(salePriceRecordVO.getWeightRecordNo());
                if(ObjectUtil.isNotNull(saleWeightRecordDO)){
                    salePriceRecordDO.setWeightRecordCode(saleWeightRecordDO.getWeightRecordCode());
                    salePriceRecordDO.setWeightRecordName(saleWeightRecordDO.getWeightRecordName());
                    salePriceRecordDO.setWeightCode(saleWeightRecordDO.getWeightCode());
                }
                salePriceRecordDO.setSaleRecordCode(priceRecordCode);
                salePriceRecordDO.setSaleCode(saleCode);
                salePriceRecordDO.setGmtCreatePin(saleBaseVO.getGmtCreatePin());
                salePriceRecordDO.setIp(saleBaseVO.getIp());
                salePriceRecordDao.insert(salePriceRecordDO);
            });
        } catch (TmsManagerException e) {
            throw TmsExceptionUtil.wrapException(e);
        }
    }

    @Override
    public SaleBaseVO selectByCode(String saleCode){
        try {
            SaleBaseVO saleBaseVO=new SaleBaseVO();
            SalePriceVO salePriceVO = salePriceDao.selectByCode(saleCode);
            if(Objects.isNull(salePriceVO)){
                throw new TmsManagerException(CommonEnum.NO_CONTENT.getCode(),CommonEnum.NO_CONTENT.getMessage());
            }
            saleBaseVO.setSalePriceVO(salePriceVO);
            SalePartSchemeVO salePartSchemeVO = salePartSchemeDao.selectByCode(saleCode);
            if(Objects.isNull(salePartSchemeVO)){
                throw new TmsManagerException(CommonEnum.NO_CONTENT.getCode(),CommonEnum.NO_CONTENT.getMessage());
            }

            List<SalePartVO> salePartVOS = salePartDao.selectByCode(salePartSchemeVO.getPartSchemeCode());
            if(CollectionUtil.isEmpty(salePartVOS)){
                throw new TmsManagerException(CommonEnum.NO_CONTENT.getCode(),CommonEnum.NO_CONTENT.getMessage());
            }
            salePartVOS.forEach(salePartVO ->{
                List<SalePartCountyVO> salePartCountyVOS = salePartCountyDao.selectByCode(salePartVO.getPartCode());
                if(CollectionUtil.isEmpty(salePartCountyVOS)){
                    throw new TmsManagerException(CommonEnum.NO_CONTENT.getCode(),CommonEnum.NO_CONTENT.getMessage());
                }
                salePartCountyVOS.forEach(salePartCountyVO ->{
                    List<SalePartCityVO> salePartCityVOS = salePartCityDao.selectByCode(salePartCountyVO.getPartCountyCode());
                    List<SalePartZipcodeVO> salePartZipcodeVOS = salePartZipcodeDao.selectByCode(salePartCountyVO.getPartCountyCode());
                    salePartCountyVO.setSalePartCityVOList(salePartCityVOS);
                    salePartCountyVO.setSalePartZipcodeVOList(salePartZipcodeVOS);
                });
                salePartVO.setSalePartCountyVOList(salePartCountyVOS);
            });
            salePartSchemeVO.setSalePartVOList(salePartVOS);
            saleBaseVO.setSalePartSchemeVO(salePartSchemeVO);

            SaleWeightVO saleWeightVO = saleWeightDao.selectByCode(saleCode);
            if(Objects.isNull(saleWeightVO)){
                throw new TmsManagerException(CommonEnum.NO_CONTENT.getCode(),CommonEnum.NO_CONTENT.getMessage());
            }
            List<SaleWeightRecordVO> saleWeightRecordVOS = saleWeightRecordDao.selectByCode(saleWeightVO.getWeightCode());
            if(CollectionUtil.isEmpty(saleWeightRecordVOS)){
                throw new TmsManagerException(CommonEnum.NO_CONTENT.getCode(),CommonEnum.NO_CONTENT.getMessage());
            }
            saleWeightVO.setSaleWeightRecordVOList(saleWeightRecordVOS);
            saleBaseVO.setSaleWeightVO(saleWeightVO);

            List<SalePriceRecordVO> salePriceRecordVOS = salePriceRecordDao.selectByCode(saleCode);
            if(CollectionUtil.isEmpty(salePriceRecordVOS)){
                throw new TmsManagerException(CommonEnum.NO_CONTENT.getCode(),CommonEnum.NO_CONTENT.getMessage());
            }
            saleBaseVO.setSalePriceRecordVOList(salePriceRecordVOS);

            return saleBaseVO;
        } catch (TmsManagerException e) {
            throw TmsExceptionUtil.wrapException(e);
        }
    }
    @SystemLog(module =SystemLog.Module.PRICE,template="priceUpdate")
    @Override
    @Transactional
    public  void updateSale(SaleBaseVO saleBaseVO){
        try {
            SalePriceVO salePriceVO = saleBaseVO.getSalePriceVO();
            SalePriceQuery query=new SalePriceQuery();
            query.setSaleProductCode(salePriceVO.getSaleProductCode());
            query.setCargoTypeCode(salePriceVO.getCargoTypeCode());
            query.setSaleCode(salePriceVO.getSaleCode());
            if (salePriceDao.queryVerifySale(query)>0){
                throw new TmsManagerException(TmsExceptionEnum.SALEPRODUCT_IS_EXIST.getCode(),TmsExceptionEnum.SALEPRODUCT_IS_EXIST.getMessage());
            }
            ;
            Date date = new Date();
            SalePriceDO salePriceDO=new SalePriceDO();
            BeanUtils.copyProperties(salePriceVO,salePriceDO);
            salePriceDO.setGmtModified(date);
            salePriceDO.setGmtModifiedPin(saleBaseVO.getGmtModifiedPin());
            salePriceDao.update(salePriceDO);//修改销售基本信息

            SalePartSchemeVO salePartSchemeVO = saleBaseVO.getSalePartSchemeVO();
            SalePartSchemeDO  salePartSchemeDO=new SalePartSchemeDO();
            BeanUtils.copyProperties(salePartSchemeVO,salePartSchemeDO);
            salePartSchemeDO.setGmtModified(date);
            salePartSchemeDO.setGmtModifiedPin(saleBaseVO.getGmtModifiedPin());
            salePartSchemeDao.update(salePartSchemeDO);//修改分区方案信息

            List<SalePartVO> salePartVOList = salePartSchemeVO.getSalePartVOList();
            List<SalePartVO> salePartVOS = salePartDao.selectByCode(salePartSchemeVO.getPartSchemeCode());
            salePartVOS.forEach(salePartVO ->{
                Optional<SalePartVO> salePartOptional = salePartVOList.stream().filter(item -> salePartVO.getPartCode().equals(item.getPartCode())).findFirst();
                if(!salePartOptional.isPresent()){//删除不存在的
                    List<SalePartCountyVO> salePartCountyVOS = salePartCountyDao.selectByCode(salePartVO.getPartCode());
                    salePartCountyVOS.forEach(salePartCountyVO ->{
                        salePartCityDao.deleteByCountyCode(salePartCountyVO.getPartCountyCode());//删除分区城市信息
                        salePartZipcodeDao.deleteByCountyCode(salePartCountyVO.getPartCountyCode());//删除分区邮编信息
                    });
                    salePartDao.deleteByPrimaryKey(salePartVO.getId());//删除分区信息
                    salePartCountyDao.deleteByPartCode(salePartVO.getPartCode());//删除分区国家信息
                }
            });

            List<String> countyList=new ArrayList<>();//国家重复
            List<String> repCountyList=new ArrayList<>();//国家重复
            Map<String,SalePartDO> salePartMap=new HashMap<>();

            salePartVOList.forEach(salePartVO ->{
                if(Objects.isNull(salePartVO.getPartCode())){//编码不存在新增
                    SalePartDO salePartDO=new SalePartDO();
                    BeanUtils.copyProperties(salePartVO,salePartDO);
                    String partCode= TmsSerializeConstant.SALEPRICE_PART_KEY+globalIdGeneratorUtil.getNextSeq(TmsSerializeConstant.SALEPRICE_PART_SEQ_KEY, TmsSerializeConstant.SEQ_INCRBY);
                    salePartDO.setPartCode(partCode);
                    salePartDO.setPartSchemeCode(salePartSchemeDO.getPartSchemeCode());
                    salePartDO.setGmtCreatePin(saleBaseVO.getGmtCreatePin());
                    salePartDO.setIp(saleBaseVO.getIp());
                    salePartDao.insert(salePartDO);//分区列表信息
                    salePartMap.put(salePartVO.getPartNo(),salePartDO);
                    List<SalePartCountyVO> salePartCountyVOList = salePartVO.getSalePartCountyVOList();
                    salePartCountyVOList.forEach(salePartCountyVO ->{
                        SalePartCountyDO salePartCountyDO=new SalePartCountyDO();
                        BeanUtils.copyProperties(salePartCountyVO,salePartCountyDO);
                        String partCountyCode= TmsSerializeConstant.SALEPRICE_PART_COUNTY_KEY+globalIdGeneratorUtil.getNextSeq(TmsSerializeConstant.SALEPRICE_PART_COUNTY_SEQ_KEY, TmsSerializeConstant.SEQ_INCRBY);
                        salePartCountyDO.setPartCountyCode(partCountyCode);
                        salePartCountyDO.setPartCode(partCode);
                        salePartCountyDO.setGmtCreatePin(saleBaseVO.getGmtCreatePin());
                        salePartCountyDO.setIp(saleBaseVO.getIp());
                        salePartCountyDO.setPartSchemeCode(salePartSchemeDO.getPartSchemeCode());
                        salePartCountyDao.insert(salePartCountyDO);//分区国家列表信息
                        if(countyList.contains(salePartCountyVO.getCountyName())){
                            repCountyList.add(salePartCountyVO.getCountyName());
                        }else{
                            countyList.add(salePartCountyVO.getCountyName());
                        }
                        List<SalePartCityVO> salePartCityVOList = salePartCountyVO.getSalePartCityVOList();
                        if(CollectionUtil.isNotEmpty(salePartCityVOList)){
                            salePartCityVOList.forEach(salePartCityVO ->{
                                SalePartCityDO salePartCityDO=new SalePartCityDO();
                                BeanUtils.copyProperties(salePartCityVO,salePartCityDO);
                                String partCityCode= TmsSerializeConstant.SALEPRICE_PART_CITY_KEY+globalIdGeneratorUtil.getNextSeq(TmsSerializeConstant.SALEPRICE_PART_CITY_SEQ_KEY, TmsSerializeConstant.SEQ_INCRBY);
                                salePartCityDO.setPartCityCode(partCityCode);
                                salePartCityDO.setPartCountyCode(partCountyCode);
                                salePartCityDO.setGmtCreatePin(saleBaseVO.getGmtCreatePin());
                                salePartCityDO.setIp(saleBaseVO.getIp());
                                salePartCityDao.insert(salePartCityDO);//分区城市列表信息
                            });
                        }
                        List<SalePartZipcodeVO> salePartZipcodeVOList = salePartCountyVO.getSalePartZipcodeVOList();
                        if(CollectionUtil.isNotEmpty(salePartZipcodeVOList)){
                            salePartZipcodeVOList.forEach(salePartZipcodeVO ->{
                                SalePartZipcodeDO salePartZipcodeDO=new SalePartZipcodeDO();
                                BeanUtils.copyProperties(salePartZipcodeVO,salePartZipcodeDO);
                                String partZipCode= TmsSerializeConstant.SALEPRICE_PART_ZIPCODE_KEY+globalIdGeneratorUtil.getNextSeq(TmsSerializeConstant.SALEPRICE_PART_ZIPCODE_SEQ_KEY, TmsSerializeConstant.SEQ_INCRBY);
                                salePartZipcodeDO.setPartZipCode(partZipCode);
                                salePartZipcodeDO.setPartCountyCode(partCountyCode);
                                salePartZipcodeDO.setGmtCreatePin(saleBaseVO.getGmtCreatePin());
                                salePartZipcodeDO.setIp(saleBaseVO.getIp());
                                salePartZipcodeDao.insert(salePartZipcodeDO);//分区邮编列表信息
                            });
                        }

                    });
                }else{
                    SalePartDO salePartDO=new SalePartDO();
                    BeanUtils.copyProperties(salePartVO,salePartDO);
                    salePartDO.setGmtModified(date);
                    salePartDO.setGmtModifiedPin(saleBaseVO.getGmtModifiedPin());
                    salePartDO.setPartSchemeCode(salePartSchemeDO.getPartSchemeCode());
                    salePartDao.update(salePartDO);//分区信息
                    salePartMap.put(salePartVO.getPartNo(),salePartDO);

                    List<SalePartCountyVO> salePartCountyVOList = salePartVO.getSalePartCountyVOList();
                    List<SalePartCountyVO> salePartCountyVOS = salePartCountyDao.selectByCode(salePartVO.getPartCode());
                    salePartCountyVOS.forEach(salePartCountyVO ->{
                        Optional<SalePartCountyVO> salePartCountyOptional = salePartCountyVOList.stream().filter(item -> salePartCountyVO.getPartCountyCode().equals(item.getPartCountyCode())).findFirst();
                        if(!salePartCountyOptional.isPresent()){//删除不存在的
                            salePartCountyDao.deleteByPrimaryKey(salePartCountyVO.getId());//删除分区国家信息
                            salePartCityDao.deleteByCountyCode(salePartCountyVO.getCountyCode());//删除分区城市信息
                            salePartZipcodeDao.deleteByCountyCode(salePartCountyVO.getCountyCode());//删除分区邮编信息
                        }
                    });
                    salePartCountyVOList.forEach(salePartCountyVO ->{
                        if(ObjectUtil.isNull(salePartCountyVO.getPartCountyCode())){//编码不存在新增
                            SalePartCountyDO salePartCountyDO=new SalePartCountyDO();
                            BeanUtils.copyProperties(salePartCountyVO,salePartCountyDO);
                            String partCountyCode= TmsSerializeConstant.SALEPRICE_PART_COUNTY_KEY+globalIdGeneratorUtil.getNextSeq(TmsSerializeConstant.SALEPRICE_PART_COUNTY_SEQ_KEY, TmsSerializeConstant.SEQ_INCRBY);
                            salePartCountyDO.setPartCountyCode(partCountyCode);
                            salePartCountyDO.setPartCode(salePartVO.getPartCode());
                            salePartCountyDO.setGmtCreatePin(saleBaseVO.getGmtCreatePin());
                            salePartCountyDO.setIp(saleBaseVO.getIp());
                            salePartCountyDO.setPartSchemeCode(salePartSchemeDO.getPartSchemeCode());
                            salePartCountyDao.insert(salePartCountyDO);//分区国家列表信息
                            if(countyList.contains(salePartCountyVO.getCountyName())){
                                repCountyList.add(salePartCountyVO.getCountyName());
                            }else{
                                countyList.add(salePartCountyVO.getCountyName());
                            }
                            List<SalePartCityVO> salePartCityVOList = salePartCountyVO.getSalePartCityVOList();
                            if(CollectionUtil.isNotEmpty(salePartCityVOList)){
                                salePartCityVOList.forEach(salePartCityVO ->{
                                    SalePartCityDO salePartCityDO=new SalePartCityDO();
                                    BeanUtils.copyProperties(salePartCityVO,salePartCityDO);
                                    String partCityCode= TmsSerializeConstant.SALEPRICE_PART_CITY_KEY+globalIdGeneratorUtil.getNextSeq(TmsSerializeConstant.SALEPRICE_PART_CITY_SEQ_KEY, TmsSerializeConstant.SEQ_INCRBY);
                                    salePartCityDO.setPartCityCode(partCityCode);
                                    salePartCityDO.setPartCountyCode(partCountyCode);
                                    salePartCityDO.setGmtCreatePin(saleBaseVO.getGmtCreatePin());
                                    salePartCityDO.setIp(saleBaseVO.getIp());
                                    salePartCityDao.insert(salePartCityDO);//分区城市列表信息
                                });
                            }
                            List<SalePartZipcodeVO> salePartZipcodeVOList = salePartCountyVO.getSalePartZipcodeVOList();
                            if(CollectionUtil.isNotEmpty(salePartZipcodeVOList)){
                                salePartZipcodeVOList.forEach(salePartZipcodeVO ->{
                                    SalePartZipcodeDO salePartZipcodeDO=new SalePartZipcodeDO();
                                    BeanUtils.copyProperties(salePartZipcodeVO,salePartZipcodeDO);
                                    String partZipCode= TmsSerializeConstant.SALEPRICE_PART_ZIPCODE_KEY+globalIdGeneratorUtil.getNextSeq(TmsSerializeConstant.SALEPRICE_PART_ZIPCODE_SEQ_KEY, TmsSerializeConstant.SEQ_INCRBY);
                                    salePartZipcodeDO.setPartZipCode(partZipCode);
                                    salePartZipcodeDO.setPartCountyCode(partCountyCode);
                                    salePartZipcodeDO.setGmtCreatePin(saleBaseVO.getGmtCreatePin());
                                    salePartZipcodeDO.setIp(saleBaseVO.getIp());
                                    salePartZipcodeDao.insert(salePartZipcodeDO);//分区邮编列表信息
                                });
                            }
                        }else{
                            SalePartCountyDO salePartCountyDO=new SalePartCountyDO();//分区国家列表信息
                            BeanUtils.copyProperties(salePartCountyVO,salePartCountyDO);
                            salePartCountyDO.setGmtModified(date);
                            salePartCountyDO.setGmtModifiedPin(saleBaseVO.getGmtModifiedPin());
                            salePartCountyDao.update(salePartCountyDO);
                            if(countyList.contains(salePartCountyVO.getCountyName())){
                                repCountyList.add(salePartCountyVO.getCountyName());
                            }else{
                                countyList.add(salePartCountyVO.getCountyName());
                            }
                            salePartCityDao.deleteByCountyCode(salePartCountyVO.getPartCountyCode());//删除分区城市信息
                            salePartZipcodeDao.deleteByCountyCode(salePartCountyVO.getPartCountyCode());//删除分区邮编信息
                            List<SalePartCityVO> salePartCityVOList = salePartCountyVO.getSalePartCityVOList();
                            if(CollectionUtil.isNotEmpty(salePartCityVOList)){
                                salePartCityVOList.forEach(salePartCityVO ->{
                                    SalePartCityDO salePartCityDO=new SalePartCityDO();//分区城市列表信息
                                    BeanUtils.copyProperties(salePartCityVO,salePartCityDO);
                                    String partCityCode= TmsSerializeConstant.SALEPRICE_PART_CITY_KEY+globalIdGeneratorUtil.getNextSeq(TmsSerializeConstant.SALEPRICE_PART_CITY_SEQ_KEY, TmsSerializeConstant.SEQ_INCRBY);
                                    salePartCityDO.setPartCityCode(partCityCode);
                                    salePartCityDO.setPartCountyCode(salePartCountyVO.getPartCountyCode());
                                    salePartCityDO.setGmtCreatePin(saleBaseVO.getGmtCreatePin());
                                    salePartCityDO.setIp(saleBaseVO.getIp());
                                    salePartCityDao.insert(salePartCityDO);
                                });
                            }
                            List<SalePartZipcodeVO> salePartZipcodeVOList = salePartCountyVO.getSalePartZipcodeVOList();
                            if(CollectionUtil.isNotEmpty(salePartZipcodeVOList)){
                                salePartZipcodeVOList.forEach(salePartZipcodeVO ->{
                                    SalePartZipcodeDO salePartZipcodeDO=new SalePartZipcodeDO();//分区邮编列表信息
                                    BeanUtils.copyProperties(salePartZipcodeVO,salePartZipcodeDO);
                                    String partZipCode= TmsSerializeConstant.SALEPRICE_PART_ZIPCODE_KEY+globalIdGeneratorUtil.getNextSeq(TmsSerializeConstant.SALEPRICE_PART_ZIPCODE_SEQ_KEY, TmsSerializeConstant.SEQ_INCRBY);
                                    salePartZipcodeDO.setPartZipCode(partZipCode);
                                    salePartZipcodeDO.setPartCountyCode(salePartCountyVO.getPartCountyCode());
                                    salePartZipcodeDO.setGmtCreatePin(saleBaseVO.getGmtCreatePin());
                                    salePartZipcodeDO.setIp(saleBaseVO.getIp());
                                    salePartZipcodeDao.insert(salePartZipcodeDO);
                                });
                            }
                        }
                    });

                }

            });

            if(CollectionUtil.isNotEmpty(repCountyList)){//国家存在重复的
                throw new TmsManagerException(TmsExceptionEnum.COUNTY_IS_EXIST.getCode(),String.format(TmsExceptionEnum.COUNTY_IS_EXIST.getMessage(), JSONUtil.toJsonStr(repCountyList)));
            }

            SaleWeightVO saleWeightVO = saleBaseVO.getSaleWeightVO();
            SaleWeightDO saleWeightDO=new SaleWeightDO();
            BeanUtils.copyProperties(saleWeightVO,saleWeightDO);
            saleWeightDO.setGmtModified(date);
            saleWeightDO.setGmtModifiedPin(saleBaseVO.getGmtModifiedPin());
            saleWeightDao.update(saleWeightDO);//重量模板信息

            List<SaleWeightRecordVO> saleWeightRecordVOList = saleWeightVO.getSaleWeightRecordVOList();
            List<SaleWeightRecordVO> saleWeightRecordVOS = saleWeightRecordDao.selectByCode(saleWeightVO.getWeightCode());
            saleWeightRecordVOS.forEach(saleWeightRecordVO ->{
                Optional<SaleWeightRecordVO> saleWeightRecordOptional = saleWeightRecordVOList.stream().filter(item -> saleWeightRecordVO.getWeightRecordCode().equals(item.getWeightRecordCode())).findFirst();
                if(!saleWeightRecordOptional.isPresent()){//删除不存在的
                    saleWeightRecordDao.deleteByPrimaryKey(saleWeightRecordVO.getId());
                }
            });
            Map<String,SaleWeightRecordDO> saleWeightRecordMap=new HashMap<>();
            saleWeightRecordVOList.forEach(saleWeightRecordVO ->{
                if(ObjectUtil.isNull(saleWeightRecordVO.getWeightRecordCode())){//编码不存在新增
                    SaleWeightRecordDO saleWeightRecordDO=new SaleWeightRecordDO();//重量模板明细
                    BeanUtils.copyProperties(saleWeightRecordVO,saleWeightRecordDO);
                    String weightRecordCode= TmsSerializeConstant.SALEPRICE_WEIGHT_RECORD_KEY+globalIdGeneratorUtil.getNextSeq(TmsSerializeConstant.SALEPRICE_WEIGHT_RECORD_SEQ_KEY, TmsSerializeConstant.SEQ_INCRBY);
                    saleWeightRecordDO.setWeightRecordCode(weightRecordCode);
                    saleWeightRecordDO.setWeightCode(saleWeightVO.getWeightCode());
                    saleWeightRecordDO.setGmtCreatePin(saleBaseVO.getGmtCreatePin());
                    saleWeightRecordDO.setIp(saleBaseVO.getIp());
                    saleWeightRecordDao.insert(saleWeightRecordDO);
                    saleWeightRecordMap.put(saleWeightRecordVO.getWeightRecordNo(),saleWeightRecordDO);
                }else{
                    SaleWeightRecordDO saleWeightRecordDO=new SaleWeightRecordDO();//重量模板明细
                    BeanUtils.copyProperties(saleWeightRecordVO,saleWeightRecordDO);
                    saleWeightRecordDO.setGmtModified(date);
                    saleWeightRecordDO.setGmtModifiedPin(saleBaseVO.getGmtModifiedPin());
                    saleWeightRecordDO.setWeightCode(saleWeightVO.getWeightCode());
                    saleWeightRecordDao.update(saleWeightRecordDO);//重量模板信息
                    saleWeightRecordMap.put(saleWeightRecordVO.getWeightRecordNo(),saleWeightRecordDO);
                }
            });


            salePriceRecordDao.deleteBySaleCode(salePriceVO.getSaleCode());//销售价格明细
            List<SalePriceRecordVO> salePriceRecordVOList = saleBaseVO.getSalePriceRecordVOList();
            salePriceRecordVOList.forEach(salePriceRecordVO ->{
                SalePriceRecordDO salePriceRecordDO=new SalePriceRecordDO();
                String priceRecordCode= TmsSerializeConstant.SALEPRICE_PRICE_RECORD_KEY+globalIdGeneratorUtil.getNextSeq(TmsSerializeConstant.SALEPRICE_PRICE_RECORD_SEQ_KEY, TmsSerializeConstant.SEQ_INCRBY);
                salePriceRecordDO.setPrice(salePriceRecordVO.getPrice());
                SalePartDO salePartDO = salePartMap.get(salePriceRecordVO.getPartNo());
                if(ObjectUtil.isNotNull(salePartDO)){
                    salePriceRecordDO.setPartCode(salePartDO.getPartCode());
                    salePriceRecordDO.setPartName(salePartDO.getPartName());
                    salePriceRecordDO.setPartSchemeCode(salePartDO.getPartSchemeCode());
                }
                SaleWeightRecordDO saleWeightRecordDO = saleWeightRecordMap.get(salePriceRecordVO.getWeightRecordNo());
                if(ObjectUtil.isNotNull(saleWeightRecordDO)){
                    salePriceRecordDO.setWeightRecordCode(saleWeightRecordDO.getWeightRecordCode());
                    salePriceRecordDO.setWeightRecordName(saleWeightRecordDO.getWeightRecordName());
                    salePriceRecordDO.setWeightCode(saleWeightRecordDO.getWeightCode());
                }
                salePriceRecordDO.setSaleRecordCode(priceRecordCode);
                salePriceRecordDO.setSaleCode(salePriceVO.getSaleCode());
                salePriceRecordDO.setGmtCreatePin(saleBaseVO.getGmtCreatePin());
                salePriceRecordDO.setIp(saleBaseVO.getIp());
                salePriceRecordDao.insert(salePriceRecordDO);
            });
        } catch (TmsManagerException e) {
            throw TmsExceptionUtil.wrapException(e);
        }
    }
    @SystemLog(module =SystemLog.Module.PRICE,template="priceDiscount")
    @Override
    @Transactional
    public void setUpDiscount(SaleBaseDiscountVO saleBaseDiscountVO) {
        try {
            salePriceDiscountDao.deleteByCode(saleBaseDiscountVO.getSaleProductCode());
            List<SalePriceDiscountVO> salePriceDiscountVOList = saleBaseDiscountVO.getSalePriceDiscountVOList();
            if(CollectionUtil.isNotEmpty(salePriceDiscountVOList)){
                for (SalePriceDiscountVO salePriceDiscountVO : salePriceDiscountVOList) {
                    String discountCode= TmsSerializeConstant.SALEPRICE_DISCOUNT_KEY+globalIdGeneratorUtil.getNextSeq(TmsSerializeConstant.SALEPRICE_DISCOUNT_SEQ_KEY, TmsSerializeConstant.SEQ_INCRBY);
                    SalePriceDiscountDO salePriceDiscountDO = new SalePriceDiscountDO();
                    BeanUtils.copyProperties(salePriceDiscountVO,salePriceDiscountDO);
                    salePriceDiscountDO.setDiscountCode(discountCode);
                    salePriceDiscountDO.setSaleProductCode(saleBaseDiscountVO.getSaleProductCode());
                    salePriceDiscountDO.setSaleProductName(saleBaseDiscountVO.getSaleProductName());
                    salePriceDiscountDO.setGmtCreatePin(saleBaseDiscountVO.getGmtCreatePin());
                    salePriceDiscountDO.setIp(saleBaseDiscountVO.getIp());
                    salePriceDiscountDao.insert(salePriceDiscountDO);
                }
            }
        } catch (DuplicateKeyException e){
            log.error("设置会员价格",e);
            throw new TmsManagerException(TmsExceptionEnum.PK_FAILED.getCode(), TmsExceptionEnum.PK_FAILED.getMessage());
        }catch (TmsManagerException e) {
            throw TmsExceptionUtil.wrapException(e);
        }
    }



}
